package com.joelaws.prototype.client.service;

import java.util.List;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.HTMLPanel;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.TextArea;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.gwtext.client.widgets.layout.AnchorLayoutData;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FormLayout;
import com.gwtext.client.widgets.map.GoogleMap;
import com.gwtext.client.widgets.map.LatLonPoint;
import com.gwtext.client.widgets.map.Marker;
import com.joelaws.prototype.client.data.DatingDetails;
import com.joelaws.prototype.client.data.Message;
import com.joelaws.prototype.client.data.NetworkingDetails;
import com.joelaws.prototype.client.data.Picture;
import com.joelaws.prototype.client.data.Rating;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.meeting.ActionInfo;
import com.joelaws.prototype.client.meeting.MeetingType;
import com.joelaws.prototype.client.rpc.Dating;
import com.joelaws.prototype.client.rpc.DatingAsync;
import com.joelaws.prototype.client.rpc.Messaging;
import com.joelaws.prototype.client.rpc.MessagingAsync;
import com.joelaws.prototype.client.util.RateIt;

class DatingTab extends Panel {

	final private User thisUser;

	final private Panel body;

	public DatingTab(User user) {
		this.thisUser = user;
		setLayout(new AnchorLayout());

		setAutoScroll(true);
		setBorder(false);
		setFrame(true);

		body = new Panel();

		add(getMainPanel());

		setTitle("Dating");
	}

	private Panel getMainPanel() {
		Panel panel = new Panel();

		final ToolbarButton profile = new ToolbarButton("Profile");
		final ToolbarButton browse = new ToolbarButton("Browse");
		final ToolbarButton search = new ToolbarButton("Search");
		final ToolbarButton ratings = new ToolbarButton("Ratings");
		final ToolbarButton meetings = new ToolbarButton("Meetings");

		ButtonListenerAdapter listener = new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				if (button == profile) {
					System.out.println("Profile");
					body.clear();
					body.add(getProfilePanel(thisUser));
					body.doLayout();
				} else if (button == browse) {
					System.out.println("Browse");
					body.clear();
					body.add(getBrowsePanel());
					body.doLayout();
				} else if (button == search) {
					System.out.println("Search");
					body.clear();
					body.add(getSearchPanel());
					body.doLayout();
				} else if (button == ratings) {
					System.out.println("Ratings");
					body.clear();
					body.add(getRatingPanel());
					body.doLayout();
				} else if (button == meetings) {
					System.out.println("Meetings");
					body.clear();
					body.add(getMeetingPanel());
					body.doLayout();
				} else {
					System.out.println("Unknown button");
				}
			}
		};

		profile.addListener(listener);
		browse.addListener(listener);
		search.addListener(listener);
		ratings.addListener(listener);
		meetings.addListener(listener);

		Toolbar toolbar = new Toolbar();
		toolbar.addButton(profile);
		toolbar.addButton(browse);
		toolbar.addButton(search);
		toolbar.addButton(ratings);
		toolbar.addButton(meetings);

		body.add(getProfilePanel(thisUser));

		panel.setTopToolbar(toolbar);
		panel.add(body);

		return panel;
	}

	private Panel getMeetingPanel() {

		final Panel panel = new Panel();
		panel.setLayout(new AnchorLayout());
		DatingAsync instance = Dating.Util.getInstance();

		instance.getMeetingUsers(thisUser.getUserID(),
				new AsyncCallback<List<User>>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("faileddddddddd in getMeetingPanel in DatingTab "
										+ caught.getMessage());
					}

					public void onSuccess(List<User> meetingUsers) {

						if (meetingUsers == null || meetingUsers.size() == 0) {
							System.out.println("Found 0 users in geMeetings");
							return;
						}

						for (User user : meetingUsers) {
							System.out.println("Found user "
									+ user.getFirstName() + " "
									+ user.getLastName());
							Panel profile = getMeetUserPanel(user);
							panel.add(profile);
							panel.add(getSpacer());
						}
						panel.doLayout();

					}
				});
		return panel;
	}

	private Panel getProfilePanel(User user) {
		Panel panel = new Panel();

		panel.setLayout(new ColumnLayout());
		panel.setBorder(false);
		panel.setFrame(true);

		panel.add(getProfileLeft(user), new ColumnLayoutData(.3));

		panel.add(getProfileRight(user), new ColumnLayoutData(.7));

		return panel;
	}

	private TabPanel getMarkerPanel(final String name, final User otherUser) {
		final TabPanel tabPanel = new TabPanel();
		tabPanel.setWidth(200);
		tabPanel.setHeight(100);

		Panel panel1 = new Panel(name);
		panel1.setPaddings(5);
		panel1.add(new Button("Select", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				DatingAsync dating = Dating.Util.getInstance();
				dating.executeAction(thisUser.getUserID(), otherUser
						.getUserID(), name, new AsyncCallback<ActionInfo>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("Failed to initiate meeting in getMarkerPanel "
										+ caught.getMessage());
					}

					public void onSuccess(ActionInfo result) {
						body.clear();
						body.add(getMeetPanel(otherUser));
						body.doLayout();

					}
				});
			}
		}));

		tabPanel.add(panel1);

		return tabPanel;
	}

	private Panel getMeetPanel(final User user) {
		final Panel panel = new Panel();
		panel.setBorder(false);
		panel.setFrame(false);
		panel.setLayout(new AnchorLayout());
		panel.setHeight(700);

		panel.add(getMiniUserPanel(user));
		panel.add(getSpacer());
		DatingAsync dating = Dating.Util.getInstance();
		dating.initMeeting(thisUser.getUserID(), user.getUserID(),
				new AsyncCallback<ActionInfo>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("Failed to initiate meeting in meet panel "
										+ caught.getMessage());

					}

					public void onSuccess(ActionInfo result) {
						if (result.getQuestion().contains(
								"Where would you like to meet for")) {
							panel.add(getGoogleMapPanel(user, result));
						} else {
							panel.add(getActionChoicePanel(user, result));
						}
						panel.doLayout();

					}
				});

		return panel;
	}

	private ComboBox getChoices(String[] choices) {

		final Store store = new SimpleStore("type", choices);
		store.load();

		final ComboBox cb = new ComboBox();
		cb.setForceSelection(true);
		cb.setMinChars(1);
		cb.setFieldLabel("Type");
		cb.setStore(store);
		cb.setDisplayField("type");
		cb.setMode(ComboBox.LOCAL);
		cb.setTriggerAction(ComboBox.ALL);
		cb.setEmptyText("Select next action");
		cb.setLoadingText("Loading...");
		cb.setTypeAhead(true);
		cb.setSelectOnFocus(true);
		cb.setWidth(200);

		cb.setHideTrigger(false);
		return cb;
	}

	private Panel getActionChoicePanel(final User user, final ActionInfo result) {
		Panel panel = new Panel();
		panel.setBorder(false);
		panel.setFrame(false);
		panel.setLayout(new AnchorLayout());
		panel.setHeight(800);
		panel.add(new Label(result.getQuestion()));
		panel.add(getSpacer());
		final Timer timer = new Timer() {

			@Override
			public void run() {
				DatingAsync dating = Dating.Util.getInstance();
				dating.initMeeting(thisUser.getUserID(), user.getUserID(),
						new AsyncCallback<ActionInfo>() {

							public void onFailure(Throwable caught) {
								System.out.println("failed to execute choice "
										+ caught.getMessage());

							}

							public void onSuccess(ActionInfo result) {
								if (result != null) {
									body.clear();
									body.add(getMeetPanel(user));
									body.doLayout();
								}
							}
						});

			}
		};
		timer.schedule(15000);

		if (result.getChoices().length > 0) {
			final ComboBox cb = getChoices(result.getChoices());
			panel.add(cb);

			Button button = new Button("Submit", new ButtonListenerAdapter() {

				public void onClick(Button button, EventObject e) {
					timer.cancel();
					DatingAsync dating = Dating.Util.getInstance();
					dating.executeAction(thisUser.getUserID(),
							user.getUserID(), cb.getValue(),
							new AsyncCallback<ActionInfo>() {

								public void onFailure(Throwable caught) {
									System.out
											.println("failed to execute choice "
													+ caught.getMessage());

								}

								public void onSuccess(ActionInfo result) {
									if (result != null) {
										body.clear();
										body.add(getMeetPanel(user));
										body.doLayout();
									}
								}
							});
				}
			});

			panel.add(button);
		}

		return panel;
	}

	private Panel getGoogleMapPanel(final User user, ActionInfo result) {
		Panel panel = new Panel();
		panel.setBorder(false);
		panel.setFrame(false);
		panel.setLayout(new AnchorLayout());
		panel.setHeight(800);
		panel.add(new Label(result.getQuestion()));
		panel.add(getSpacer());
		final ComboBox cb = getChoices(new String[] { "Cancel" });
		panel.add(cb);

		Button button = new Button("Submit", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {

				DatingAsync dating = Dating.Util.getInstance();
				dating.executeAction(thisUser.getUserID(), user.getUserID(), cb
						.getValue(), new AsyncCallback<ActionInfo>() {

					public void onFailure(Throwable caught) {
						System.out.println("failed to execute choice "
								+ caught.getMessage());

					}

					public void onSuccess(ActionInfo result) {
						body.clear();
						body.add(getMeetPanel(user));
						body.doLayout();

					}
				});
			}
		});
		panel.add(button);
		panel.add(getSpacer());
		GoogleMap mapPanel = new GoogleMap();
		mapPanel.setWidth(500);
		mapPanel.setHeight(500);

		LatLonPoint home = new LatLonPoint(40.706762, -74.010000);
		mapPanel.setCenterAndZoom(home, 15);

		mapPanel.setAutoScroll(false);
		mapPanel.setClosable(false);

		MeetingType choice = result.getType();
		if (choice.equals(MeetingType.Food)) {
			LatLonPoint myPoint = new LatLonPoint(40.704290, -74.010193);
			Marker marker = new Marker(myPoint);
			marker.setInfoBubble(getMarkerPanel("Ulysses", user));
			mapPanel.addMarker(marker);
		} else if (choice.equals(MeetingType.Coffee)) {
			LatLonPoint myPoint = new LatLonPoint(40.705884, -74.012060);
			Marker marker = new Marker(myPoint);
			marker.setInfoBubble(getMarkerPanel("Starbucks", user));
			mapPanel.addMarker(marker);
		} else if (choice.equals(MeetingType.Park)) {
			LatLonPoint myPoint = new LatLonPoint(40.703119, -74.014978);
			Marker marker = new Marker(myPoint);
			marker.setInfoBubble(getMarkerPanel("Battery Park", user));
			mapPanel.addMarker(marker);
		} else {
			System.out.println("Unknown selection.");
		}

		panel.add(mapPanel);
		return panel;
	}

	private Panel getProfileLeft(final User user) {
		final Panel panel = new Panel();
		panel.setHeight(800);
		panel.setLayout(new AnchorLayout());

		if (thisUser != user) {
			final Button message = new Button("Send a Message",
					new ButtonListenerAdapter() {

						public void onClick(Button button, EventObject e) {
							System.out.println("Sending a message");
							body.clear();
							body.add(getComposePanel(user));
							body.doLayout();
						}
					});

			final Button date = new Button("Meet for a Date",
					new ButtonListenerAdapter() {

						public void onClick(Button button, EventObject e) {
							System.out.println("Meet for a Date");
							body.clear();
							body.add(getMeetPanel(user));
							body.doLayout();
						}
					});

			DatingAsync instance = Dating.Util.getInstance();
			instance.getRating(thisUser.getUserID(), user.getUserID(),
					new AsyncCallback<Rating>() {

						public void onFailure(Throwable caught) {
							System.out
									.println("faileddddddddd in getOtherProfileLeft1 in DatingTab "
											+ caught.getMessage());

						}

						public void onSuccess(Rating result) {
							System.out.println("Back from result");
							short rating = 0;
							if (result != null) {
								rating = result.getRating();
								System.out.println("setting result to "
										+ rating);
							}
							panel.add(getImage(user));
							panel.add(getSpacer());
							panel.add(getRateItPanel(user, rating));
							panel.add(getSpacer());
							panel.add(message);
							panel.add(getSpacer());
							panel.add(date);
							panel.add(getSpacer());
							panel.add(getAd());

							panel.doLayout();
						}
					});
		} else {

			panel.add(getImage(user));

			panel.add(getSpacer());
			panel.add(getAd());
		}

		return panel;
	}

	private RateIt getRateItPanel(final User user, short rating) {
		Image whiteStarImage = new Image("images/whiteStar.jpg");
		Image yellowStarImg = new Image("images/yellowStar.jpg");
		Image whiteDonutImg = new Image("images/whiteDonut.jpg");
		Image redDonutImg = new Image("images/yellowDonut.jpg");
		Image redStarImg = new Image("images/redStar.jpg");
		final RateIt rateIt = new RateIt(rating/* current rating */, 5 /*
		 * max
		 * rating
		 */, whiteStarImage, yellowStarImg, whiteDonutImg, redDonutImg,
				redStarImg);

		rateIt.addChangeListener(new ChangeListener() {

			public void onChange(Widget sender) {
				short rating = rateIt.getUserRating();
				System.out.println("rated " + rating);
				DatingAsync instance = Dating.Util.getInstance();
				instance.rateUser(thisUser.getUserID(), user.getUserID(),
						rating, new AsyncCallback<Object>() {

							public void onFailure(Throwable caught) {
								System.out
										.println("faileddddddddd in getOtherProfileLeft2 in DatingTab "
												+ caught.getMessage());
							}

							public void onSuccess(Object arg0) {
								System.out.println("Rated user successfully");
							}
						});
			}
		});
		return rateIt;
	}

	private Panel getComposePanel(final User user) {
		FormPanel formPanel = new FormPanel();
		formPanel.setAutoScroll(true);
		formPanel.setLayout(new FormLayout());

		final TextField to = new TextField("To", "to");
		to.setValue(user.getFirstName());
		formPanel.add(to);

		final TextField subject = new TextField("Subject", "subject");
		formPanel.add(subject);

		final TextArea body = new TextArea("Body", "body");
		formPanel.add(body);

		formPanel.add(new Button("Send", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				Message message = new Message(thisUser.getUserID(), user
						.getUserID(), subject.getText(), body.getText());
				MessagingAsync instance = Messaging.Util.getInstance();
				instance.storeMessage(message, new AsyncCallback<Object>() {

					public void onFailure(Throwable caught) {
						System.out.println(caught.getMessage());
						MessageBox.alert("Message failed");
					}

					public void onSuccess(Object arg0) {
						MessageBox.alert("Message Sent");
					}
				});
			}
		}));
		return formPanel;
	}

	private Panel getProfileRight(User user) {
		Panel panel = new Panel();
		panel.setLayout(new AnchorLayout());

		panel
				.add(new HTMLPanel(user.getFirstName() + " "
						+ user.getLastName()));

		NetworkingDetails details = user.getNetworkingDetails();
		if (details != null) {
			panel.add(new HTMLPanel((details.isMale()) ? "Male" : "Female"));

			if (details.getBirthday() != null) {
				panel.add(new HTMLPanel("Birthday: "
						+ details.getBirthday().toString() + "<br/>"));
			}
			if (details.getDegree() != null) {
				panel.add(new HTMLPanel("Degree: "
						+ details.getDegree().toString() + "<br/>"));
			}
			if (details.getIncome() != null) {
				panel.add(new HTMLPanel("Income: "
						+ details.getIncome().toString() + "<br/>"));
			}
			if (details.getEmployment() != null) {
				panel.add(new HTMLPanel("Employment: "
						+ details.getEmployment().toString() + "<br/>"));
			}
			if (details.getReligion() != null) {
				panel.add(new HTMLPanel("Religion: "
						+ details.getReligion().toString() + "<br/>"));
			}

		}

		DatingDetails datingDetails = user.getDatingDetails();
		if (datingDetails != null) {
			if (datingDetails.getEyeColor() != null) {
				panel.add(new HTMLPanel("Eye Color: "
						+ datingDetails.getEyeColor().toString() + "<br/>"));
			}
			if (datingDetails.getHairColor() != null) {
				panel.add(new HTMLPanel("Hair Color: "
						+ datingDetails.getHairColor().toString() + "<br/>"));
			}
			if (datingDetails.getHeight() != null) {
				panel.add(new HTMLPanel("Height: "
						+ datingDetails.getHeight().toString() + "<br/>"));
			}
		}

		return panel;
	}

	private Panel getBrowsePanel() {
		final Panel panel = new Panel();

		panel.setLayout(new AnchorLayout());

		updateBrowsePanel(panel, 0);

		return panel;
	}

	private void updateBrowsePanel(final Panel panel, final int start) {
		DatingAsync instance = Dating.Util.getInstance();

		instance.getBrowseUsers(thisUser.getUserID(), start,
				new AsyncCallback<List<User>>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("faileddddddddd in getBrowsePanel in DatingTab "
										+ caught.getMessage());
					}

					public void onSuccess(final List<User> users) {
						if (users == null || users.size() == 0) {
							System.out
									.println("Found no users in getBrowse call");
							return;
						}
						panel.clear();

						for (User user : users) {
							if (user.getUserID() != thisUser.getUserID()) {
								Panel profile = getMiniUserPanel(user);
								panel.add(profile);
								panel.add(getSpacer());
							}
						}

						Button next = new Button("Next");

						next.addListener(new ButtonListenerAdapter() {
							public void onClick(Button button, EventObject e) {
								updateBrowsePanel(panel, start + 5);
							}
						});
						panel.add(next);

						panel.doLayout();

					}
				});
	}

	private Panel getSearchPanel() {
		final Panel panel = new Panel();
		panel.setLayout(new AnchorLayout());
		panel.setBorder(false);
		panel.setFrame(false);

		final Panel resultsPanel = new Panel();
		resultsPanel.setLayout(new AnchorLayout());
		resultsPanel.setBorder(false);
		resultsPanel.setFrame(false);

		final TextField searchField = new TextField("", "search");
		searchField.setHideLabel(true);

		panel.add(searchField, new AnchorLayoutData("100%"));

		final Button search = new Button("Search");

		ButtonListenerAdapter listener = new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				String searchString = searchField.getText();
				System.out.println("Searching the Unknown for "
						+ searchField.getText());
				if (searchString == null || searchString.length() == 0) {
					return;
				}

				DatingAsync search = Dating.Util.getInstance();
				search.search(thisUser.getUserID(), searchString,
						new AsyncCallback<List<User>>() {

							public void onFailure(Throwable caught) {
								System.out
										.println("failed in search panel due to "
												+ caught.getMessage());

							}

							public void onSuccess(List<User> users) {

								if (users == null) {
									return;
								}
								resultsPanel.clear();
								for (User user : users) {
									Panel profile = getMiniUserPanel(user);
									resultsPanel.add(profile);
									resultsPanel.add(getSpacer());
								}
								if (users.size() == 0) {
									resultsPanel.add(new HTMLPanel(
											"<br/>0 results"));
								}
								System.out.println("Found " + users.size()
										+ " users");
								resultsPanel.doLayout();
							}
						});
			}
		};

		search.addListener(listener);
		panel.add(search);

		panel.add(resultsPanel);

		return panel;
	}

	private Panel getMeetUserPanel(final User user) {
		Panel panel = new Panel();

		panel.setLayout(new ColumnLayout());
		panel.setHeight(150);
		FormPanel leftPanel = new FormPanel();

		leftPanel.setLayout(new AnchorLayout());

		Image userImage = getImage(user);
		userImage.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				System.out.println("Viewing Profile " + user.getFirstName());
				body.clear();
				body.add(getProfilePanel(user));
				body.doLayout();
			}
		});
		leftPanel.add(userImage);

		panel.add(leftPanel, new ColumnLayoutData(.4));

		final Panel infoPanel = new Panel();

		infoPanel.setLayout(new AnchorLayout());
		final Label name = new Label("Name");
		name.setText(user.getFirstName() + " " + user.getLastName());

		final Button date = new Button("View Meeting",
				new ButtonListenerAdapter() {

					public void onClick(Button button, EventObject e) {
						System.out.println("View Meeting");
						body.clear();
						body.add(getMeetPanel(user));
						body.doLayout();
					}
				});

		infoPanel.add(name);
		infoPanel.add(date);

		panel.add(infoPanel, new ColumnLayoutData(.6));
		return panel;
	}

	private Panel getMiniUserPanel(final User user) {
		Panel panel = new Panel();

		panel.setLayout(new ColumnLayout());
		panel.setHeight(150);
		FormPanel leftPanel = new FormPanel();

		leftPanel.setLayout(new AnchorLayout());

		Image userImage = getImage(user);
		userImage.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				System.out.println("Viewing Profile " + user.getFirstName());
				body.clear();
				body.add(getProfilePanel(user));
				body.doLayout();
			}
		});
		leftPanel.add(userImage);

		panel.add(leftPanel, new ColumnLayoutData(.4));

		final Panel infoPanel = new Panel();

		infoPanel.setLayout(new AnchorLayout());
		final Label name = new Label("Name");
		name.setText(user.getFirstName() + " " + user.getLastName());
		final Button viewProfile = new Button("View Profile",
				new ButtonListenerAdapter() {

					public void onClick(Button button, EventObject e) {
						System.out.println("Viewing Profile "
								+ user.getFirstName());
						body.clear();
						body.add(getProfilePanel(user));
						body.doLayout();
					}
				});

		final Button message = new Button("Send a Message",
				new ButtonListenerAdapter() {

					public void onClick(Button button, EventObject e) {
						System.out.println("Sending a message");
						body.clear();
						body.add(getComposePanel(user));
						body.doLayout();
					}
				});

		final Button date = new Button("Meet for a Date",
				new ButtonListenerAdapter() {

					public void onClick(Button button, EventObject e) {
						System.out.println("Meet for a Date");
						body.clear();
						body.add(getMeetPanel(user));
						body.doLayout();
					}
				});

		DatingAsync instance = Dating.Util.getInstance();
		instance.getRating(thisUser.getUserID(), user.getUserID(),
				new AsyncCallback<Rating>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("faileddddddddd in getOtherProfileLeft1 in DatingTab "
										+ caught.getMessage());

					}

					public void onSuccess(Rating result) {
						System.out.println("Back from result");
						short rating = 0;
						if (result != null) {
							rating = result.getRating();
							System.out.println("setting result to " + rating);
						}
						infoPanel.add(name);
						infoPanel.add(getRateItPanel(user, rating));
						infoPanel.add(viewProfile);
						infoPanel.add(message);
						infoPanel.add(date);
						infoPanel.doLayout();
					}
				});
		panel.add(infoPanel, new ColumnLayoutData(.6));
		return panel;
	}

	private Panel getRatingPanel() {

		final Panel panel = new Panel();
		panel.setLayout(new AnchorLayout());
		DatingAsync instance = Dating.Util.getInstance();

		instance.getRatedUsers(thisUser.getUserID(),
				new AsyncCallback<List<User>>() {

					public void onFailure(Throwable caught) {
						System.out
								.println("faileddddddddd in getRatingPanel in DatingTab "
										+ caught.getMessage());
					}

					public void onSuccess(List<User> ratedUsers) {

						if (ratedUsers == null || ratedUsers.size() == 0) {
							System.out.println("Found 0 users in getRatings");
							return;
						}

						for (User user : ratedUsers) {
							System.out.println("Found user "
									+ user.getFirstName() + " "
									+ user.getLastName());
							Panel profile = getMiniUserPanel(user);
							panel.add(profile);
							panel.add(getSpacer());
						}
						panel.doLayout();

					}
				});
		return panel;
	}

	private Panel getSpacer() {
		return new HTMLPanel("<br/><br/>");
	}

	private Image getAd() {
		Image ad;
		if (Math.random() > .5) {
			ad = new Image("images/ad1.gif");
		} else {
			ad = new Image("images/ad2.gif");
		}

		ad.setSize("100px", "400px");
		return ad;

	}

	private Image getImage(User user) {

		String loc;
		Picture pic = user.getNetworkingService().getPicture();
		if (pic != null) {
			loc = pic.getLocation();
		} else {
			loc = "images/space_ghost.png";
		}
		System.out.println("Pic location is " + loc);
		Image image = new Image(loc);
		image.setSize("100px", "100px");
		return image;

	}
}
